package com.hzy;

import com.hzy.base.PersistenceFactory;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

@SpringBootApplication
public class ShadowfiendApplication {

	public static void main(String[] args) {
		SpringApplication.run(ShadowfiendApplication.class, args);
	}

	@Configuration
	@EnableGlobalMethodSecurity(prePostEnabled = true)
	protected static class SecurityConfiguration extends WebSecurityConfigurerAdapter{

		@Autowired
		private DataSource dataSource;

		@Override
		protected void configure(HttpSecurity http) throws Exception {
			http.httpBasic().and()
					.authorizeRequests().anyRequest().permitAll().and()
					.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
					.csrf().disable();
//					.addFilterBefore(new TokenAuthenticationFilter(), BasicAuthenticationFilter.class);
		}

//		@Autowired
//		protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{
//
//			auth.jdbcAuthentication()
//					.passwordEncoder(passwordEncoder())
//					.usersByUsernameQuery("select id, password, enabled where username = ?")
//					.dataSource(dataSource);
//		}
		@Override
		protected void configure(AuthenticationManagerBuilder auth) throws Exception {
			auth.jdbcAuthentication()
					.passwordEncoder(passwordEncoder())
					.usersByUsernameQuery("select id, password, enabled where username = ?")
					.rolePrefix("ROLE_")
					.dataSource(dataSource);
		}

		@Bean
		public PasswordEncoder passwordEncoder(){
			return new BCryptPasswordEncoder();
		}
	}

	@Configuration
	protected static class PersistenceFactoryConfig{


		@Bean
		public PersistenceFactory persistenceFactory(DataSource dataSource){
			PersistenceFactory persistenceFactory = new PersistenceFactory();
			persistenceFactory.setPlatformTransactionManager(platformTransactionManager(dataSource));
			persistenceFactory.setSqlSessionFactory(configureSqlSessionFatory(dataSource));
			return persistenceFactory;
		}

		@Bean
		public PlatformTransactionManager platformTransactionManager(DataSource dataSource) {
			return new DataSourceTransactionManager(dataSource);
		}

		@Bean
		public SqlSessionFactory configureSqlSessionFatory(DataSource dataSource){
			SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
			sessionFactoryBean.setDataSource(dataSource);
			sessionFactoryBean.setConfigLocation(new DefaultResourceLoader().getResource("classpath:mybatis-config.xml"));

			SqlSessionFactory sqlSessionFactory = null;
			try {
				sqlSessionFactory = sessionFactoryBean.getObject();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return sqlSessionFactory;
		}

	}

}
